let App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_id: null,
        order: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.order_id = options.order_id;
        this.getOrderDetail(options.order_id);
    },

    /**
     * 获取订单详情
     */
    getOrderDetail: function (order_id) {
        let _this = this;
        App.visit('integral/orderDetail', {
          order_id:order_id
        }, '', function (result) {
            console.log('订单详情',result);
            _this.setData({
                order: result.data
            });
        });
    },

    /**
     * 跳转到商品详情
     */
    goodsDetail: function (e) {
        let goods_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../jf/jf_index?id=' + goods_id
        });
    },

    /**
     * 取消订单
     */
    cancelOrder: function (e) {
        let _this = this;
        let order_id = _this.data.order_id;
        var user_id = wx.getStorageSync("user_id");
        wx.showModal({
            title: "提示",
            content: "确认取消订单？",
            success: function (o) {
                if (o.confirm) {
                    App.visit('good/cancel', {
                        order_id:order_id,
                        user_id:user_id
                    }, '',function (result) {
                        wx.navigateBack();
                    });
                }
            }
        });
    },

    /**
     * 发起付款
     */
    payOrder: function (e) {
        let _this = this;
        let order_id = _this.data.order_id;
        var user_id = wx.getStorageSync("user_id");
        // 显示loading
        wx.showLoading({ title: '正在处理...', });
        App.visit('good/pay', {
            order_id:order_id,
            user_id:user_id
        }, 'POST',function (result) {
            console.error('支付',result);
            // 发起微信支付
            wx.requestPayment({
                timeStamp: result.data.timeStamp,
                nonceStr: result.data.nonceStr,
                package: 'prepay_id=' + result.data.prepay_id,
                signType: 'MD5',
                paySign: result.data.paySign,
                success: function (res) {
                    _this.getOrderDetail(order_id);
                }
            });
            wx.hideLoading();
        });
    },

    /**
     * 确认收货
     */
    receipt: function (e) {
        let _this = this;
        let order_id = _this.data.order_id;
        var user_id = wx.getStorageSync("user_id");
        wx.showModal({
            title: "提示",
            content: "确认收到商品？",
            success: function (o) {
                if (o.confirm) {
                    App.visit('integral/receipt', { order_id:order_id,user_id:user_id }, '',function (result) {
                        _this.getOrderDetail(order_id);
                    });
                }
            }
        });
    },

});